﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace kis.UC
{
    public partial class BaseForm
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dg"></param>
        public static void InitDataGridView(DataGridView dg)
        {
            if (dg == null)
            {
                return;
            }

            dg.AllowUserToAddRows = false;
            dg.ReadOnly = true;
            dg.AllowUserToResizeColumns = true;
            dg.AllowUserToResizeRows = false;
            dg.MultiSelect = false;
            dg.BackgroundColor = Color.White;

            dg.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            dg.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            foreach (DataGridViewColumn col in dg.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            dg.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;

            dg.RowPostPaint += dg_RowPostPaint;

            foreach (DataGridViewColumn col in dg.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            dg.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            DoubleBufferedDataGirdView(dg, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void dg_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dg = sender as DataGridView;

            using (SolidBrush b = new SolidBrush(dg.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, CultureInfo.CurrentUICulture),
                e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }
        }

        /// <summary>
        /// 双缓冲，解决闪烁问题
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="flag"></param>
        private static void DoubleBufferedDataGirdView(DataGridView dgv, bool flag)
        {
            Type dgvType = dgv.GetType();

            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);

            pi.SetValue(dgv, flag, null);
        }

        /// <summary>
        /// DataGridView 单元格内容单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="action"></param>
        public void dgCellContentAction<T>(object sender, DataGridViewCellEventArgs e, Action<DataGridView, T, string> action)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0)
            {
                return;
            }

            DataGridView dg = sender as DataGridView;

            if (dg.CurrentRow == null)
            {
                return;
            }

            var item = dg.CurrentRow.DataBoundItem;

            string name = dg.Columns[e.ColumnIndex].Name;

            action(dg, (T)item, name);
        }

        /// <summary>
        /// DataGridView 单元格事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="action"></param>
        public void dgContentAction<T>(object sender, DataGridViewCellEventArgs e, Action<DataGridView, T> action)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0)
            {
                return;
            }

            DataGridView dg = sender as DataGridView;

            var item = dg.CurrentRow.DataBoundItem;

            if (dg.CurrentRow == null)
            {
                return;
            }

            action(dg, (T)item);
        }
    }
}